package UIComponents
{
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	
	public class MakingPanel extends Sprite
	{
		private var _nowPage:int=1;/*当前页数*/
		private var _maxPage:int;/*最大页数*/
		private var _itemsAry:Array;/*物品数组*/
		
		public function MakingPanel()
		{
			init();
		}
		
		private function init():void
		{
			inintLister();
		}
		private function inintLister():void
		{
			(this.getChildByName("buttonLeft") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftHandler);
			(this.getChildByName("buttonRight") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightHandler);
		}
		private function turnLeftHandler(e:ComponentEvent):void
		{
			this.nowPage-=1;
		}
		
		private function turnRightHandler(e:ComponentEvent):void
		{
			this.nowPage+=1;
		}
		
		private function updatePage():void
		{
			(this.getChildByName("txt_page") as TextField).text=this.nowPage+"/"+this.maxPage;
			setLeftAndRightVisible();
		}
		
		/*getter & setter*/
		public function get nowPage():int
		{
			return _nowPage;
		}
		
		public function setItemsAry(itemsAry:Array):void
		{
			this._itemsAry=[];
			this._itemsAry=itemsAry;
			if(!this.itemsAry.length)
			{
				this.maxPage=1;
			}
			else
			{
				this.maxPage=int((this.itemsAry.length-1)/3)+1;
			}
			if(this.nowPage>this.maxPage)
			{
				this.nowPage=this.maxPage;
			}
			else
			{
				this.reshow();
			}
		}
		
		public function get mkBtnForArrow():DisplayObject
		{
			return this.getMakingListFrame(0).mkBtn;
		}
		
		private function getMakingListFrame(index:int):MakingFrameListPanel
		{
			if(index<0 || index>2)throw new Error("索引出错");
			return (this.getChildByName("makingList"+(index+1))) as MakingFrameListPanel;
		}
		
		public function reshow():void
		{
			for(var i:int=0;i<3;i++)
			{
				var mklist:MakingFrameListPanel=this.getMakingListFrame(i);
				mklist.reset();
				mklist.mflmb.visible=true;
				var collectItem:IMission=this.itemsAry[i+(this.nowPage-1)*3] as IMission;
				if(null!=collectItem)
				{
					mklist.setData(collectItem);
//					mklist.visible=true;
				}//else{
//					mklist.visible=false;
//				}
			}
		}
		
		public function set nowPage(value:int):void
		{
			if(value==this.nowPage)return;
			if(value<1 || value>this.maxPage)return;
			_nowPage = value;
			this.updatePage();
			this.reshow();
		}
		
		public function get maxPage():int
		{
			return _maxPage;
		}
		
		public function set maxPage(value:int):void
		{
			if(value<0) return;
			_maxPage = value;
			this.updatePage();
		}
		
		public function get itemsAry():Array
		{
			return _itemsAry;
		}
		
		/**
		 *设置左、右翻页按扭是否可见 
		 */
		private function setLeftAndRightVisible():void
		{
			var filter:ColorMatrixFilter = new ColorMatrixFilter([0.3,0.6,0,0,0,0.3,0.6,0,0,0,0.3,0.6,0,0,0,0,0,0,1,0]) ;
			
			var noShowLeft:Boolean		=		this._nowPage==1;
			var noShowRight:Boolean		=		this._nowPage==this._maxPage;
			
			if(!noShowLeft){
				//				(this.getChildByName("buttonLeft") as MyButton).visible				=		true;
				(this.getChildByName("buttonLeft") as MyButton).mouseChildren			    =		true;
				(this.getChildByName("buttonLeft") as MyButton).mouseEnabled			    =		true;
				(this.getChildByName("buttonLeft") as MyButton).filters					=		null;
			}
			else
			{
				//				(this.getChildByName("buttonLeft") as MyButton)						=		false;
				(this.getChildByName("buttonLeft") as MyButton).mouseChildren				=		false;
				(this.getChildByName("buttonLeft") as MyButton).mouseEnabled				=		false;
				(this.getChildByName("buttonLeft") as MyButton).filters					=		[filter];
			}
			
			if(!noShowRight){
				//				(this.getChildByName("buttonRight") as MyButton).visible				=		true;
				(this.getChildByName("buttonRight") as MyButton).mouseChildren		=		true;
				(this.getChildByName("buttonRight") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("buttonRight") as MyButton).filters				=		null;
			}
			else
			{
				//				(this.getChildByName("buttonRight") as MyButton).visible				=		false;
				(this.getChildByName("buttonRight") as MyButton).mouseChildren		=		false;
				(this.getChildByName("buttonRight") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("buttonRight") as MyButton).filters				=		[filter];
			}
		}
	}
}